
import PlatformUtil from "./PlatformUtil";
import httpUtils from "./XmlHttpTool";
import StartGameView from "./StartGameView";
import xorEncryptionData from "./Tool/xor";
import allVariableJs from "./allVariable";
cc.Class({
  extends: cc.Component,

  properties: {
    loginBtn: cc.Button,
    infoLabel: {
      default: null,
      type: cc.Label,
    },
    startGamejs: StartGameView,
    userInfoDic: "",
    showHubProjess: cc.Node,
    SvprogressPrefab: cc.Prefab,
    logonode: cc.Node,
    guestBtn: cc.Button,
    userTokenCode: ""
  },



  onLoad() {

    var changebig2 = cc.scaleTo(0.2, 1.2);

    var finish = cc.callFunc(function () {

      var changesmail2 = cc.scaleTo(0.1, 1.0);
      this.logonode.runAction(changesmail2)

    }, this);

    var seq = cc.sequence(changebig2, finish)

    this.logonode.runAction(seq)
    console.log("token==", allVariableJs.userToken)
    if (allVariableJs.userToken) {
      console.log("facebook已经登录过了", allVariableJs.userToken)
      PlatformUtil.init();
      PlatformUtil.platform.init(this);
      PlatformUtil.platform.params["token"] = allVariableJs.userToken
      this.startGamejs.getUserInfoHttp()
      this.node.active = false

    } else {

      console.log("facebook第一次登录")
      var playerID = FBInstant.player.getID();
      console.log("准备登录用户ID==", playerID)
      PlatformUtil.init();
      PlatformUtil.platform.init(this);
      this.loginGame(playerID)

    }
  },

  loginGameSence: function () {

    var playerID = FBInstant.player.getID();
    console.log("准备登录用户ID==", playerID)
    PlatformUtil.init();
    PlatformUtil.platform.init(this);
    this.loginGame(playerID)

  },

  loginGame: function (userIdString) {

    // if(cc.isValid(this.showHubProjess)){
    //     this.showHubProjess.destroy();
    //   }
    //   this.showHubProjess = cc.instantiate(this.SvprogressPrefab);
    //   this.node.addChild(this.showHubProjess);
    //   this.showHubProjess.getComponent('SVProgressView').init("Logging in..Please Waiting")

    // let action = cc.sequence(cc.fadeIn(0.6), cc.delayTime(10), cc.fadeOut(1));
    // this.showHubProjess.runAction(action);

    var resultstr = xorEncryptionData.encryptionData(userIdString, PlatformUtil.platform.params)

    console.log("code加密", resultstr)
    var that = this
    httpUtils.httpPost("/quick_users/wxregister", { "regtype": 1, "code": resultstr }, function (res) {

      console.log("获取token", res["token"])
      var tokenCode = res["token"]
     
      PlatformUtil.platform.setStorage({key:"tokenCode", value:res["token"]})
      var tokenstring = PlatformUtil.platform.getStorage("tokenCode")
      console.log("本地token", tokenstring)
    
      allVariableJs.userToken =tokenstring
      console.log("全新字典1", JSON.stringify(PlatformUtil.platform.params))
      PlatformUtil.platform.params["token"] = res["token"]
      console.log("全新字典2", JSON.stringify(PlatformUtil.platform.params))
      that.save_UserInfo(userIdString)


    }, function (failres) {

      console.log("时间超时1", JSON.stringify(failres))
      if (cc.isValid(that.showHubProjess)) {
        that.showHubProjess.destroy();
      }
      that.showHubProjess = cc.instantiate(that.SvprogressPrefab);
      that.node.addChild(that.showHubProjess);
      that.showHubProjess.getComponent('SVProgressView').init("登录超时,请重新登录")

      let action = cc.sequence(cc.fadeIn(0.6), cc.delayTime(5), cc.fadeOut(1));
      that.showHubProjess.runAction(action);
      that.loginBtn.active = true

    },
      function () {
        console.log("时间超时2")
        if (cc.isValid(that.showHubProjess)) {
          that.showHubProjess.destroy();
        }
        that.showHubProjess = cc.instantiate(that.SvprogressPrefab);
        that.node.addChild(that.showHubProjess);
        that.showHubProjess.getComponent('SVProgressView').init("登录超时,请重新登录")

        let action = cc.sequence(cc.fadeIn(0.6), cc.delayTime(5), cc.fadeOut(1));
        that.showHubProjess.runAction(action);
        that.loginBtn.active = true

      }
    )
  },

  save_UserInfo: function (userIdString) {

    var that = this

    var playerName = FBInstant.player.getName();
    var playPhotoUrl = FBInstant.player.getPhoto();
    httpUtils.httpPost("/users/save_user_info", { "avatar": playPhotoUrl, "name": playerName, "gender": "0" }, function (res) {

      console.log("存储用户信息", JSON.stringify(res))
      that.startGamejs.getUserInfoHttp()
      that.node.active = false
        
    }, function (failres) {
      console.log("时间超时3")
      if (cc.isValid(that.showHubProjess)) {
        that.showHubProjess.destroy();
      }
      that.showHubProjess = cc.instantiate(that.SvprogressPrefab);
      that.node.addChild(that.showHubProjess);
      that.showHubProjess.getComponent('SVProgressView').init("登录超时,请重新登录")
      that.loginBtn.active = true
      let action = cc.sequence(cc.fadeIn(0.6), cc.delayTime(5), cc.fadeOut(1));
      that.showHubProjess.runAction(action);

    },
      function () {
        console.log("时间超时4")
        if (cc.isValid(that.showHubProjess)) {
          that.showHubProjess.destroy();
        }
        that.showHubProjess = cc.instantiate(that.SvprogressPrefab);
        that.node.addChild(that.showHubProjess);
        that.showHubProjess.getComponent('SVProgressView').init("登录超时,请重新登录")
        that.loginBtn.active = true

        let action = cc.sequence(cc.fadeIn(0.6), cc.delayTime(5), cc.fadeOut(1));
        that.showHubProjess.runAction(action);

      })

  },

  start() {
    console.log("fb小游戏初始化")
  },

});
